package 二叉树系列;

import java.util.*;

public class RightSideView {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> resList = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        if(root==null) return resList;
        deque.push(root);
        while(!deque.isEmpty()){
            int len = deque.size();

            for(int i=0;i<len;i++){
                TreeNode treeNode = deque.pollFirst();
                if(treeNode.left!=null) deque.addLast(treeNode.left);
                if(treeNode.right!=null) deque.addLast(treeNode.right);
                if(i==len-1) resList.add(treeNode.val);
            }
        }
        return resList;
    }
}
